
// Definition for a binary tree node.
function TreeNode(val, left, right) {
    this.val = (val===undefined ? 0 : val)
    this.left = (left===undefined ? null : left)
    this.right = (right===undefined ? null : right)
}

/**
 * @param {TreeNode} root
 * @param {TreeNode} subRoot
 * @return {boolean}
 */
var isSubtree = function(root, subRoot) {
  let flag = false;
  function isEqual(node1, node2) {
    if(!node1 && !node2) return true;
    if(!node1 || !node2) return false;
    if(node1.val != node2.val) return false;
    return isEqual(node1.left, node2.left) && isEqual(node1.right, node2.right);
  }
  function preView(node) {
    if(!node) return ;
    if(node.val === subRoot.val && !flag) {
      flag = isEqual(node, subRoot);
    }
    preView(node.left);
    preView(node.right);
  }
  preView(root);

  return flag;
};
